////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.workplaces.display {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.display.GradientType;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormatAlign;
	
	import kiwi.components.Checkbox;
	import kiwi.components.DragObject;
	import kiwi.components.IDropTarget;
	import kiwi.events.CallbackErrorEvent;
	import kiwi.events.CallbackEvent;
	import kiwi.utils.Validation;
	
	import plantool.data.QueryErrorResult;
	import plantool.data.QueryResult;
	import plantool.display.skins.EmptySkin;
	import plantool.events.OrderEvent;
	import plantool.helpers.TextFieldHelper;
	import plantool.modules.orders.data.Order;
	import plantool.modules.orders.data.Orderplanning;
	import plantool.modules.orders.data.Orderrow;
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	[Event(name="change", type="flash.events.Event")]
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class WorkplaceOrderrow extends WorkplaceDisplayObject implements IWorkplaceOrderrow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _order:Order				= null;
		private var _orderRow:Orderrow			= null;
		private var _hotspot:DragObject			= null;
		private var _orderData:Orderplanning 	= null;
		private var _tfOnr:TextField			= null;
		private var _tfTime:TextField			= null;
		private var _cbReady:Checkbox			= null;
		private var _height:Number				= 15;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		public function get orderData():Orderplanning {
			return _orderData;
		}
		public function set orderData(value:Orderplanning):void {
			_orderData = value;
			
			//dispose old order
			if (Validation.isSet(_order)) {
				_order.removeEventListener(OrderEvent.ROWS_LOADED	, onRows_loaded);
				_order = null;
			}
			if (Validation.isSet(_order)) {
				_orderRow = null;
			}
			//load new order
			orderApi.getOrderByID(_orderData.order_id, onComplete_getOrder, onError_getOrder);
		}
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function WorkplaceOrderrow() {
			super();
		}
		
		public override function dispose():void {
			//dispose hourdata
			if (Validation.isSet(_orderData)) {
				_orderData = null;
			}
			
			//dispose hotspot
			if (Validation.isSet(_hotspot)) {
				//dispose hotspot
				_hotspot.dispose();
				_hotspot = null;
			}
			
			if (Validation.isSet(_cbReady)) {
				//dispose hotspot
				_cbReady.dispose();
				_cbReady = null;
			}
			
			if (Validation.isSet(_order)) {
				_order.removeEventListener(OrderEvent.ROWS_LOADED	, onRows_loaded);
				_order = null;
			}
			
			if (Validation.isSet(_orderRow)) {
				_orderRow = null;
			}
			
			//dispatch event so parent knows we're disposed
			dispatchEvent(new Event(Event.CHANGE));
			
			//dispose object
			super.dispose();
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onComplete_getOrder(result:QueryResult):void {
			if (Validation.isSet(result) && Validation.isSet(result.data) && result.data.length == 1 && result.data[0] is Order) {
				//store loaded order
				_order = result.data[0];
				
				//check if rows loaded
				if (_order.rowsLoaded) {
					setOrderRow();
				} else {
					_order.addEventListener(OrderEvent.ROWS_LOADED	, onRows_loaded);
				}
				
				//invalidate
				invalidate();
			}
		}
		
		private function onMouse_cbReady(e:CallbackEvent):void {
			switch (e.userData.type) {
				case MouseEvent.CLICK:
					_orderData.ready = !_orderData.ready;
					orderApi.updateOrderrowPlanning(_orderData);
					break;
			}
		}
		
		private function onError_getOrder(error:QueryErrorResult):void {
			
		}
		
		private function onDrop_order(target:IDropTarget, item:DragObject):void {
			if (target is IWorkplaceDay) {
				var day:IWorkplaceDay = target as IWorkplaceDay;
				
				_orderData.related_id 	= day.workplace.id;
				_orderData.fromDate		= day.date;
				
				orderApi.updateOrderrowPlanning(_orderData, null, null);
			}
		}
		
		private function onValidateDrop_order(target:IDropTarget, item:DragObject):Boolean {
			if (target is IWorkplaceDay) {
				var day:IWorkplaceDay = target as IWorkplaceDay;
				return (day.workplace.id != _orderData.related_id) || (day.workplace.id == _orderData.related_id && !dateApi.validateDay(day.date, _orderData.fromDate));
			}
			return false;
		}
		
		private function onRows_loaded(result:OrderEvent):void {
			_order.removeEventListener(OrderEvent.ROWS_LOADED	, onRows_loaded);
			setOrderRow();
		}
		
		private function setOrderRow():void {
			for (var i:uint = 0; i < _order.orderrows.length; i++) {
				if (_order.orderrows[i].id == _orderData.id) {
					_orderRow = _order.orderrows[i];
					break;
				}
			}
			
			invalidate();
		}
		
		protected override function init():void {
			//create tf for time
			_tfTime			= TextFieldHelper.createInfoTf(TextFieldAutoSize.LEFT, TextFormatAlign.LEFT, false, false, 0);
			_tfTime.x		= parent.width - 2 - _tfTime.width;
			addChild(_tfTime);
			
			//create tf for ordernumber
			_tfOnr				= TextFieldHelper.createInfoTf(TextFieldAutoSize.LEFT, TextFormatAlign.LEFT, false, false, 0);
			_tfOnr.x			= _height;
			addChild(_tfOnr);
			
			//create hotspot
			_hotspot			= new DragObject();
			_hotspot.skin		= new EmptySkin();
			_hotspot.width 		= parent.width - 2;
			_hotspot.height		= _height;
			addChild(_hotspot);
			
			//drag properties
			_hotspot.dragItem		= this;
			_hotspot.droppedHandler			= onDrop_order;
			_hotspot.validateDropHandler	= onValidateDrop_order;
			_hotspot.dropTargets.push(IWorkplaceDay);
			
			//add checkbox
			_cbReady 		= new Checkbox();
			_cbReady.width 	= _height - 4;
			_cbReady.height = _height - 4;
			_cbReady.x		= 2;
			_cbReady.y		= 2;
			_cbReady.mouseHandler = onMouse_cbReady;
			addChild(_cbReady);
			
			//draw hour
			invalidate();
		}
		
		public override function draw():void {
			//create button graphics
			graphics.clear();
			if (Validation.isSet(_orderData) && _orderData.ready) {
				graphics.beginFill(0);
				_tfTime.textColor 	= 0xFFFFFF;
				_tfOnr.textColor 	= 0xFFFFFF;
			} else if (Validation.isSet(_order) && Validation.isSet(_order.meta) && _order.meta.bool_field_1) {
				graphics.beginFill(0xFF0000);
				_tfTime.textColor 	= 0;
				_tfOnr.textColor 	= 0;
			} else {
				graphics.beginFill(0xCCCCCC);
				_tfTime.textColor 	= 0;
				_tfOnr.textColor 	= 0;
			}
			graphics.drawRect(0, 0, parent.width -2, _height);
			graphics.endFill();
			
			var myMatrix:Matrix = new Matrix();
			myMatrix.createGradientBox(parent.width -2, _height, Math.PI / 4, 0, 0);
			var colors:Array = [0xFFFFFF, 0xFFFFFF];
			colors = [0xFFFFFF, 0xFFFFFF];
			var alphas:Array = [0.2, 0.4];
			var ratios:Array = [0, 255];
			
			graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, myMatrix);
			graphics.drawRect(0, 0, parent.width -2, _height);
			graphics.endFill();
			
			if (Validation.isSet(_order)) {
				_tfOnr.text = _order.onr;
			}
			
			if (Validation.isSet(_orderData)) {
				_tfTime.text 		= _orderData.time.toString();
				_hotspot.visible	= !_orderData.ready;
				_cbReady.checked	= _orderData.ready;
			}
			_tfTime.x		= parent.width - 2 - _tfTime.width - 2;
		}
	}
}